package tr.edu.yildiz.ce.dap.acm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.util.tracker.ServiceTracker;

import tr.edu.yildiz.ce.dap.acm.gen.Cache;
import tr.edu.yildiz.ce.dap.acm.gen.CachedComponent;
import tr.edu.yildiz.ce.dap.acm.service.CacheManagerService;
import tr.edu.yildiz.ce.dap.acm.service.impl.CacheManagerServiceImpl;
import tr.edu.yildiz.ce.dap.acm.util.CacheProperties;
import tr.edu.yildiz.ce.dap.acm.util.exception.CacheConfigurationFaultException;

public class DownloadServlet extends HttpServlet{

	ServiceTracker serviceTracker;
	
	public DownloadServlet(ServiceTracker serviceTracker) {
		this.serviceTracker = serviceTracker;
	}
	
	
	public DownloadServlet() {
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		CacheManagerService cacheManagerService = new CacheManagerServiceImpl();
		String hashedId = req.getParameter("hashedId");
		try {
			Cache cache = cacheManagerService.getCache();
			List<CachedComponent> ccList = cache.getCachedComponent();
			for (CachedComponent cachedComponent : ccList) {
				if (cachedComponent.getComponentId().equals(hashedId)){
					cacheManagerService.writeCachedComponent(resp.getWriter(), cachedComponent);
					break;
				}
			}
		} catch (CacheConfigurationFaultException e) {
			e.printStackTrace();
		}
	}
	
}
